/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package processer;

import log.LogDef;
import main.BotManager;
import utils.Debug;

/**
 *
 * @author Administrator
 */
public class BaCayPlayer extends Player {

    private int countOpenCard;

    public BaCayPlayer(RoBot parent, GameData gameData, BotManager manager) {
        super(parent, gameData, manager);
        initValue();
        initObject();

        if (isViewer) {
            initForViewer();
        }
    }

    private void initForViewer() {
    }

    private void setCardShare(int[] cardIds) {
    }

    private void initValue() {
        maxPlayer = gameData.getMaxPlayer();
        myViewId = gameData.getMyViewId();
        playerId = gameData.getPlayerId();
        isViewer = (myViewId < maxPlayer ? false : true);
        if (isViewer) {
            playerId = 0;
        }
        numberPlayers = gameData.getNumberPlayers();
        if (isViewer) {
            playerId = 0;
        }
        Debug.d(gameData.myName + ",isview :" + isViewer);
        gameData.setGameDisplay(this);
        countOpenCard = 0;
    }

    private void initObject() {
        initArr();
    }

    private void initArr() {
    }

//------------------------------------------------------------------------------
    private void printArr2(int[] arr, int index) {
        for (int i = 0; i < index; i++) {
            Debug.d("arr :" + arr[i]);
        }
    }

    private void sortIncreaseBySuit(int[] cardIds, int length) {
        for (int i = 0; i < length - 1; i++) {
            for (int j = i + 1; j < length; j++) {
                if (cardIds[i] / 13 > cardIds[j] / 13) {
                    int tg = cardIds[i];
                    cardIds[i] = cardIds[j];
                    cardIds[j] = tg;
                } else if (cardIds[i] / 13 == cardIds[j] / 13) {
                    if (cardIds[i] % 13 > cardIds[j] % 13) {
                        int tg = cardIds[i];
                        cardIds[i] = cardIds[j];
                        cardIds[j] = tg;
                    }
                }
            }
        }
    }

    private int findFlush(int[] cardIds, int length, int cardCheckEat, int[] resultId) {

        Debug.d("find flush ");
//        printArr2(cardIds, length);
        sortIncreaseBySuit(cardIds, length);
        for (int i = 0; i < length; i++) {
            if (i > 0) {
                if (cardCheckEat == cardIds[i] + 1
                        && cardCheckEat == cardIds[i - 1] + 2
                        && cardCheckEat / 13 == cardIds[i] / 13
                        && cardCheckEat / 13 == cardIds[i - 1] / 13) {
                    resultId[0] = cardIds[i - 1];
                    resultId[1] = cardIds[i];
                    resultId[2] = cardCheckEat;
                    return 3;
                }
            }
            if (i < length - 1) {
                if (cardCheckEat == cardIds[i] - 1
                        && cardCheckEat == cardIds[i + 1] - 2
                        && cardCheckEat / 13 == cardIds[i] / 13
                        && cardCheckEat / 13 == cardIds[i + 1] / 13) {

                    resultId[0] = cardCheckEat;
                    resultId[1] = cardIds[i];
                    resultId[2] = cardIds[i + 1];
                    return 3;
                }
            }
            if (i > 0) {
                if (cardCheckEat == cardIds[i] - 1
                        && cardCheckEat == cardIds[i - 1] + 1
                        && cardCheckEat / 13 == cardIds[i] / 13
                        && cardCheckEat / 13 == cardIds[i - 1] / 13) {
                    resultId[0] = cardIds[i - 1];
                    resultId[1] = cardCheckEat;
                    resultId[2] = cardIds[i];
                    return 3;
                }
            }

        }
        return 0;

    }

//-----------------------------------------------------------------------------
    @Override
    public void doLoop() {
        super.doLoop();
    }

    @Override
    void doFightCard() {
        Debug.d("do fight card");
        if (countOpenCard >= 0) {
            for (int i = 0; i < 3; i++) {
                parent.request.openBaCay(playerId, i);
            }
            parent.request.finishOpenCardBaCay();
            countOpenCard = -1;
        }
    }

    @Override
    public void processMesage(String[] part) {
        super.processMesage(part);
        Debug.d("ba cay process :" + part[0]);

        if (part[0].equals("SE")) {
            gameData.setNumberPlayerInTable(part);
            parent.changeProcesser(RoBot.TABLE_CHOOSER);
            return;
        }
        if (part[0].equals("INFO")) {
            gameData.setInfoData(part);
            return;
        }
        if (part[0].equals("RE")) {
            gameData.setReArrData(part);
            return;
        }
        if (part[0].equals("CM")) {
            try {
                int money = Integer.parseInt(part[2]);
                int id = Integer.parseInt(part[1]);
                gameData.changeMoney(id, money);
            } catch (Exception ex) {
                StringBuilder buffer = new StringBuilder();
                for (int i = 0; i < part.length; i++) {
                    buffer.append(part[i]).append("|");
                }
                ex.printStackTrace();
                manager.logUserStatus(ex.toString() + buffer.toString(), LogDef.LOG_LEVEL.LOG_ERROR);

            }
            return;
        }
        if (part[0].equals("FCM")) {
            countOpenCard = 0;
            Debug.d("is view :" + isViewer);
            if (isViewer) {
                parent.request.joinGame(gameData.myName);
            }
            parent.changeProcesser(RoBot.WAITING);
            return;

        }

    }
}
